<script setup>
 import { ref, computed } from 'vue'
  import { onLoad } from '@dcloudio/uni-app'
  import { storeToRefs } from 'pinia'
  import { useTaskStore } from '@/stores/task'
  import slVehicleViolation from './components/vehicle-violation'
  import slVehicleBreakdown from './components/vehicle-breakdown'
  import slVehicleAccident from './components/vehicle-accident'
import { recordRequest } from '../../apis/task'
import { showFailToast, showToast } from '../../utils'

  // 回车时间
  const dataTimePicker = ref('')
  const transportTaskId = ref('')
  //出车时间
  const startTime = ref('')
// 回车时间（临时性的）
  const endTime = ref('')
   // 获取地址参数
    onLoad((query) => {
     if(query.transportTaskId){
       transportTaskId.value = query.transportTaskId
     }
     if(query.actualDepartureTime){
       startTime.value = query.actualDepartureTime
     }
    })

 // 提交回车登记
  async function onFormSubmit() {
    const taskStore = useTaskStore()
    if(endTime.value.length === 0){
      return showFailToast('回车时间为空')
    }
    // 过滤掉图片多余的数据，只保留 url
    const { accidentImagesList, faultImagesList } = taskStore.recordData
    // 事故照片
    taskStore.recordData.accidentImagesList = accidentImagesList.map(({ url }) => {
      return { url }
    })
    // 故障照片
    taskStore.recordData.faultImagesList = faultImagesList.map(({ url }) => {
      return { url }
    })

    console.log('taskStore.recordData22',taskStore.recordData);
    taskStore.recordData.id = transportTaskId.value
    taskStore.recordData.startTime = startTime.value
    taskStore.recordData.endTime = endTime.value
    recordRequest(taskStore.recordData,res => {
      if(res.code === 200){
        showToast("回车登记成功")
      // 跳转到任务列表
        uni.reLaunch({ url: '/pages/task/index' })
      }else{
        showFailToast("回车登记失败:" + res.msg)
      }
    })
  }
</script>
<template>
  <view class="page-container">
    <scroll-view class="scroll-view" scroll-y>
      <view class="scroll-view-wrapper">
        <uni-list class="base-info">
          <uni-list-item show-arrow title="出车时间">
            <template v-slot:footer>
              <uni-datetime-picker v-model="startTime">
                <view class="picker-value">{{ startTime || '请选择' }}</view>
              </uni-datetime-picker>
            </template>

          </uni-list-item>
          <uni-list-item show-arrow title="回车时间">
          <template v-slot:footer>
            <uni-datetime-picker v-model="endTime">
              <view class="picker-value">{{ endTime || '请选择' }}</view>
            </uni-datetime-picker>
          </template>

        </uni-list-item>
        </uni-list>
        <sl-vehicle-violation></sl-vehicle-violation>
        <sl-vehicle-breakdown></sl-vehicle-breakdown>
        <sl-vehicle-accident></sl-vehicle-accident>
      </view>
    </scroll-view>
    <view class="toolbar">
      <button  @click="onFormSubmit" class="button">提交登记</button>
    </view>
  </view>
</template>

<style lang="scss" scoped>
  @import './index.scss';
</style>
